package com.uenpay.im.ui.old.activity.company;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.uenpay.im.BroadcastAction;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.callback.IOnBottomDialogListener;
import com.uenpay.im.weight.DialogUtility;

/**
 * @author SL
 * @created 2016-2-22 16:33:01
 * @copyRight http://www.qiyunxin.com
 * @function TODO
 */
public class ManagerCompanyActivity extends Activity {

	private CompanyHandle mCompanyHandle = new CompanyHandle();
	private int entid;
	private String entname;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		IMApplication.getInstance().addActivity(this);
		setContentView(R.layout.activity_manager_company_layout);
		entid = getIntent().getIntExtra("entid", 0);
		entname = getIntent().getStringExtra("entname");
		initView();
		initListener();
	}

	private void initView() {
		((TextView) findViewById(R.id.title_textview))
				.setText(R.string.company_manager);
	}

	private void initListener() {
		findViewById(R.id.back_layout).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						finish();
					}
				});
		findViewById(R.id.department_manager_layout).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						Intent intent = new Intent(ManagerCompanyActivity.this,
								DepartmentsListActivity.class);
						intent.putExtra("is_manager", true);
						intent.putExtra("entid", entid);
						intent.putExtra("superior_department", entname);
						startActivity(intent);
					}
				});
		findViewById(R.id.editor_company_layout).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						Intent intent = new Intent(ManagerCompanyActivity.this,
								CreateCompanyActivity.class);
						intent.putExtra("entid", entid);
						intent.putExtra("entname", entname);
						startActivity(intent);
					}
				});

		findViewById(R.id.delete_company_layout).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						deleteCompanyDialog();
					}
				});
	}

	private void deleteCompanyDialog() {
		DialogUtility.showDialog(this, R.string.delete_company_dialog,
				R.string.sure, R.string.cancel, true,
				new IOnBottomDialogListener() {

					@Override
					public void onClicked() {
						mCompanyHandle.deleteCompany(entid,
								new CompanyHandle.ICompanyResultListener() {

									@Override
									public void onResult(int status, String msg) {
										if (status == HttpInvokeResultCode.RESULT_OK) {
											sendBroadcast(new Intent(
													BroadcastAction.ACTION_REFRESH_ORG));
											finish();
										} else if (!TextUtils.isEmpty(msg)) {
											IMApplication.showToast(msg);
										}
									}
								});
					}
				});
	}

	@Override
	public void onBackPressed() {
		finish();
		super.onBackPressed();
	}

	@Override
	protected void onDestroy() {
		IMApplication.getInstance().removeActivity(this);
		super.onDestroy();
	}
}
